<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);


class contest_model extends model
{

    public function __construct()
    {
        $this->db_config = pc_base::load_config('database');
        $this->db_setting = 'default';
        $this->table_name = 'contest';
        parent::__construct();
    }
    //查询最新的赛事、活动
    public function now_contest_select($contestid=0){
        $tree = pc_base::load_sys_class('tree');
        $contest_model = pc_base::load_model('contest_model');
        //获取当前站点的可用赛事、活动
        $contest_list = $contest_model->select(" status = 1 AND siteid = " . get_siteid());
        $array = array();
        foreach($contest_list as $v) {
            $v['name'] = L($v['name']);
            $v['selected'] = $v['id'] == $contestid ? 'selected' : '';
            $array[] = $v;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_categorys = $tree->get_tree(0, $str);
        return $select_categorys;
    }

    
    //根据 parentid来获取所有的子赛事、活动
    public function child_id($parentid,$type=1){
        //type:1数组；2字符串。parentid：父级id
        $contest_list = $this->select("parentid= " .$parentid,"id");
        foreach($contest_list as $k=>$v){
            $return[]=$v['id'];
        }
        $return[]=$parentid;
        if($type==1){
            return $return;
        }

        if($type==2){
            $rtn="";
            if (is_array($return) ){
                $rtn.= implode(',', $return);
            }
            return $rtn;
        }

    }

    //检查特定的parentid赛事、活动是否含有子级
    public function check_child($parentid){
        $contest_list = $this->select("parentid= " .$parentid." and status=1","id");
        if(!empty($contest_list)){
            return true;
        }else{
            return false;
        }

    }
}